import { createStore } from 'vuex'
import permissionToast from './permissionToast'
import {
	userApi
} from '@/http/userApi.js'
import { USER_TOKEN,REFRESH_TOKEN } from '@/constants/local-storage-key-const';

export default createStore({
	modules: {
		permissionToast,
	},
	state: {
		statusBarHeight: 10,
		titleBarWidth: 350,
		titleBarHeight: 40,
		menuButtonHeight: 32,
		longitude: '114.250918',
		latitude: '30.545012',
		loginStatus: false,
		notice: uni.getStorageSync('notice') || false,
		UnReadCount: uni.getStorageSync('UnReadCount') || 0,
		redirect: 1,
		AlipayAccount: null,
		ReadMessageInfo: [],
		isRecomend: false,
		isSquare: false,
		isBell: false,
		isPersonal: false,
		user: {
			id: '0',
			accountId: '',
			accountLabel: '',
			accountStatus: 1,
			accountName: '',
			accountType: 1,
			availableAmount: 0,
			avatar: 'http://mozhe.oss-cn-hangzhou.aliyuncs.com/mz/20240523/49927.jpg',
			nickname: '饮酒知冷暖',
			phone: '',
			realName: "王龙星",
			signature: "总有人会成为现在的我，那就让我来吧！"
		},
		isRefreshing: false
	},
	mutations: {
		setCoin(state, coin) {
			if (coin.operate) {
				state.user.availableAmount = state.user.availableAmount + coin.num
			} else {
				state.user.availableAmount = state.user.availableAmount - coin.num
			}
		},
		setHotRecommend(state, bl) {
			state.isRecomend = bl
			// let blData = bl?'1':'0'
			// uni.setStorageSync('isRecomend',blData)
		},
		setSquare(state, bl) {
			state.isSquare = bl
		},
		setBell(state, bl) {
			state.isBell = bl
		},
		setPersonal(state, bl) {
			state.isPersonal = bl
		},
		setUnReadCount(state, count) {
			state.UnReadCount = count
		},
		setNotice(state, status) {
			state.notice = status
		},
		setLogin(state, info) {
			state.loginStatus = true
			state.user = info
		},
		setAlipayAccount(state, Account) {
			state.AlipayAccount = Account
		},
		setPosition(state, city) {
			state.user.locatedCity = city
		},
		refresh(state, refresh) {
			state.user.nickname = refresh.nickname
			state.user.avatar = refresh.avatar
			state.user.gender = refresh.gender
		},
		logOut(state) {
			state.loginStatus = false
			state.user.avatar = 'http://mozhe.oss-cn-hangzhou.aliyuncs.com/mz/20240523/49927.jpg'
			uni.clearStorageSync()
		},
		setStatusBarHeight(state, statusBarHeight) {
			state.statusBarHeight = statusBarHeight
		},
		setWxHeader(state, wxHeader) {
			state.statusBarHeight = wxHeader.statusBarHeight
			state.menuButtonHeight = wxHeader.menuButtonHeight
			state.titleBarWidth = wxHeader.titleBarWidth
			state.titleBarHeight = wxHeader.titleBarHeight
		},
		setItude(state, position) {
			state.longitude = position.longitude
			state.latitude = position.latitude
		},
	},
	actions: {
		async refreshToken({
			commit,
			state
		}, {
			token
		}) {
			if (state.isRefreshing) return; // 防止重复刷新
			state.isRefreshing = true;
			try {
				// 判断token 存在
				if (token) {
					const udata = await userApi.getTokenRefresh(token)
					commit('setLogin', udata.userAccountVo)
					uni.setStorageSync(USER_TOKEN, udata.accessToken)
					uni.setStorageSync(REFRESH_TOKEN, udata.refreshToken)
					if (!udata.userAccountVo.gender) {
						uni.navigateTo({
							url: '/pagesMain/Initial-info/Initial-info',
						})
					} else {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				} else {
					commit("logOut")
					uni.redirectTo({
						url: '/pagesMain/mplogin/mplogin'
					})
				}
			} finally {
				state.isRefreshing = false;
			}
		}
	}
})